/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
import __config from '../config/env'

const request = (url, method, data, showLoading) => {
  let _url = __config.basePath + url
  return new Promise((resolve, reject) => {
    if (showLoading) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'app-id': wx.getAccountInfoSync().miniProgram.appId,
        'third-session': getApp().globalData.thirdSession != null ? getApp().globalData.thirdSession : ''
      },
      success(res) {
        if (res.statusCode == 200) {
          if (res.data.code != 200) {
            console.log(res.data)
            wx.showModal({
              title: '提示',
              content: res.data.msg ? res.data.msg : '没有数据' + '',
              success() {

              },
              complete() {
                if (res.data.code == 60001) {
                  //session过期，则清除过期session，并重新加载当前页
                  getApp().globalData.thirdSession = null
                  wx.reLaunch({
                    url: getApp().getCurrentPageUrlWithArgs()
                  })
                }
              }
            })
            reject(res.data.msg)
          }
          resolve(res.data)
        } else if (res.statusCode == 404) {
          wx.showModal({
            title: '提示',
            content: '接口请求出错，请检查手机网络',
            success(res) {

            }
          })
          reject()
        } else {
          console.log(res)
          wx.showModal({
            title: '提示',
            content: res.errMsg + ':' + res.data.message + ':' + res.data.msg,
            success(res) {

            }
          })
          reject()
        }
      },
      fail(error) {
        console.log(error)
        wx.showModal({
          title: '提示',
          content: '接口请求出错：' + error.errMsg,
          success(res) {

          }
        })
        reject(error)
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  })
}

module.exports = {
  request,
  login: (data) => {//小程序登录接口
    return request('/weixin/api/ma/wxuser/login', 'post', data, false)
  },
  loginScan: (data) => {//小程序登录接口
    return request('/weixin/api/ma/wxuser/loginscan', 'post', data, false)
  },
  getIndex: () => {//小程序首页
    return request('/weixin/api/index', 'get', null, false)
  },
  getPhone: (encrypdata, ivdata, sessionkey) => {//小程序电话号码解析
    return request('/weixin/api/deciphering?encrypdata=' + encrypdata + '&ivdata=' + ivdata + '&sessionkey=' + sessionkey, 'get', null, false)
  },
  getPhoneNumber: (appId, code) => {//小程序电话号码解析
    return request('/weixin/api/phonenumber?appId=' + appId + '&code=' + code, 'get', null, false)
  },
  wxUserGet: (data) => {//微信用户查询
    return request('/weixin/api/ma/wxuser', 'get', null, false)
  },
  wxUserPut: (data) => {//微信用户更新
    return request('/weixin/api/ma/wxuser', 'put', data, false)
  },
  wxUserSave: (data) => {//微信用户新增
    return request('/weixin/api/ma/wxuser', 'post', data, true)
  },
  sendMessage: (data) => {//发送订阅消息
    return request('/weixin/sub/addsenddata', 'post', data, true)
  },
  scanAction: (data) => { //已经扫描
    return request('/weixin/api/ma/wxuser/scan', 'post', data, true)
  },
  getDevice: (data) => {//首页的设备信息
    return request('/weixin/api/ma/wxuser/device', 'get', null, false)
  },
  getDeviceWx: (data) => {//定时首页的设备信息
    return request('/weixin/api/ma/wxuser/deviceWx', 'post', data, false)
  },
  getAlarm: (data) => {//告警页面
    return request('/weixin/api/getAlarm?orderLevel=' + data.level + '&orderTime=' + data.time, 'get', null, false)
  },
  updateAlarmRead: (data) => {//告警设置已读
    return request('/weixin/api/updateAlarmRead?faultId=' + data, 'get', null, false)
  },
  dismissAlarm: (data) => {//告警处理
    return request('/weixin/api/dismissAlarm', 'post', data, false)
  },
  getAlarmDetail: (data) => {//告警详情
    return request('/weixin/api/getAlarmDetail?faultId=' + data, 'get', null, false)
  },
  getDeviceDetail: (data) => {//设备详细信息
    return request('/weixin/api/getDevice?deviceId=' + data, 'get', null, false)
  },
  updateDevice: (data) => { //更新设备状态
    return request('/weixin/api/updateDevice', 'post', data, true)
  },
  updateSensor: (data) => { //更新传感器状态
    return request('/weixin/api/updateSensor', 'post', data, true)
  },
    goodsPage: (data) => {//商品列表
        return request('/weixin/api/ma/goodsspu/page', 'get', data, false)
    },
    getDeviceModel: (data) => {//根据设备型号获取型号信息
        return request('/weixin/api/deviceModel?modelCode=' + data, 'get', null, false)
    },
    getDocument: (data) => {//根据文档id获取文档信息
        return request('/weixin/api/document?documentId=' + data, 'get', null, false)
    },
    getDangerousList: (data) => {//获取试剂柜列表
        return request('/dangerous/api/getDeviceList', 'get', null, false)
    },
    getDangerousDetail: (code) => {//获取试剂柜详情
        return request('/dangerous/api/getDeviceDetail/' + code, 'get', null, false)
    },
    getDangerousStoreDetail: (cabinetId, doorId) => {//获取试剂柜库存详情
        let data = ""
        if (doorId != null) {
            data = '?cabinetId=' + cabinetId + '&doorId=' + doorId
        } else {
            data = '?cabinetId=' + cabinetId
        }
        return request('/dangerous/api/getDeviceStoreDetail' + data, 'get', null, false)
    },
    goodsSearch: (searchKey) => {//商品搜索列表
        return request('/app/api//searchScriptKill?searchKey=' + searchKey, 'get', null, false)
    },
    addFeedback: (data) => {//新增帮助与反馈
        return request('/weixin/api/feedback', 'post', data, false)
    },
    myFeedback: (data) => {//我的帮助与反馈
        return request('/weixin/api/myFeedback', 'get', null, false)
    },
    commonFeedback: (data) => {//常见问题
    return request('/weixin/api/commonFeedback', 'get', null, false)
  },
  oneKeyRepair: (data) => {//一键报修
    return request('/weixin/api/oneKeyRepair', 'post', data, false)
  },


}